// modules/userModules/pages/addressAdd/addressAdd.js
import {msgTip} from  '../../../../utils/MsgTipApi'
import {addOrEdit,findCode, findPage} from  '../../../../api/address'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    provinceName: '',
    provinceCode: '',
    cityName: '',
    cityCode: '',
    districtName: '',
    districtCode: '',
    address: '',
    fullAddress: '',
    isDefault: false,
    phone: '',
    userName: '',
  },
  onPickChange(event){
    let [provinceName,cityName,districtName] = event.detail.value;
    let [provinceCode,cityCode,districtCode] = event.detail.code;
    this.setData({
      fullAddress:''
    })
    this.setData({
      provinceName,
      cityName,
      districtName,
      provinceCode,
      cityCode,
      districtCode,
      fullAddress: provinceName + ' '+ cityName + ' ' + districtName + " "+this.data.address
    })
  },
  onAddressChange(event){
    let {value} = event.detail
    this.setData({
      fullAddress: ''
    })
    this.setData({
      address: value,
      fullAddress: this.data.provinceName + ' '+ this.data.cityName + ' ' + this.data.districtName +  ' '+ this.data.address
    })
  },
  /**
   * 新增或者编辑收货地址
   */
  async addOrUpdate(){
    if(!this.data.provinceName || !this.data.cityName || !this.data.districtName || !this.data.userName || !this.data.phone || !this.data.address){
      msgTip({title:'请填写完整'})
    }
    let reqData = {
      ...this.data
    }
    let data  = await addOrEdit(reqData);
    wx.navigateBack({delta:-1})

  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let id = options.id;
    if(id){
    let data = await findCode(id)
    this.setData({
      id: data.id,
      provinceName: data.provinceName,
      provinceCode: data.provinceCode,
      cityName: data.cityName,
      cityCode: data.cityCode,
      districtName: data.districtName,
      districtCode: data.districtCode,
      address: data.address,
      fullAddress: data.fullAddress,
      isDefault: data.isDefault,
      phone: data.phone,
      userName: data.userName,
    })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})